package ingredientsClient;

import javax.xml.soap.SOAPMessage;

public class Main {
    
    public static void sumar( int arg0, int arg1 )
    {
        // creo el mensaje
        SoapMessage message = new SoapMessage();
        // creo un metodo
        SoapMethod method = new SoapMethod( "add", "ser", "http://server/" );
        // le agrego los parametros al metodo
        method.addParameter( new SoapParameter( "arg0", ""+arg0 ) );
        method.addParameter( new SoapParameter( "arg1", ""+arg1 ) );
        
        try
        {
            //agrego el metodo al mensaje
            message.addMethod( method );
        }
        catch( Exception ex )
        {
            System.err.println( "could not create message " + ex.toString() );
            System.exit( -1 );
        }
        
        // un poco de debug
        System.out.println( message.toString() );
        // creo el cliente que consume el ws apuntando a mi host
        // lo que puede ser cambiado :)
        Client client = new Client( "http://localhost:8765/Server/Server", "Server", "ServerPort", "http://localhost:8765/Server/Server" );
        
        // invoco el metodo pidiendole a message que genere el xml
        SOAPMessage response = client.invoke( message.getMessage() );
        
        try
        {
            // imprimo la salida
            response.writeTo( System.out );
            System.out.println();
        }
        catch( Exception ex )
        {
            // mensaje estilo "gato" :P
            System.err.println( "error masivo" );
            System.exit( -1 );
        }
        
    }
    
    public static void saludar( String nombre )
    {
        SoapMessage message = new SoapMessage();
        SoapMethod method = new SoapMethod( "hello", "s", "http://server/" );
        method.addParameter( new SoapParameter( "arg0", nombre ) );
        
        try
        {
            message.addMethod( method );
        }
        catch( Exception ex )
        {
            System.err.println( "could not create message " + ex.toString() );
            System.exit( -1 );
        }
        
        System.out.println( message.toString() );
        Client client = new Client( "http://localhost:8765/Server/Server", "Server", "ServerPort", "http://localhost:8765/Server/Server" );
        
        SOAPMessage response = client.invoke(message.toString() );
        
        try
        {
            response.writeTo( System.out );
            System.out.println();
        }
        catch( Exception ex )
        {
            System.err.println( "error masivo" );
            System.exit( -1 );
        }
    }
    
    public static void fac( double farenheit )
    {
        SoapMessage message = new SoapMessage();
        SoapMethod method = new SoapMethod( "FahrenheitToCelcius", "tem", "http://webservices.daelab.net/temperature" );
        method.addParameter( new SoapParameter( "nFahrenheit",""+farenheit ) );
        
        try
        {
            message.addMethod( method );
        }
        catch( Exception ex )
        {
            System.err.println( "could not create message " + ex.toString() );
            System.exit( -1 );
        }
        
        System.out.println( message.toString() );
        Client client = new Client( "http://webservices.daelab.net/temperatureconversions/TemperatureConversions.wso", "TemperatureConversions", "TemperatureConversionsSoap", "http://webservices.daelab.net/temperatureconversions/TemperatureConversions.wso" );
        
        SOAPMessage response = client.invoke(message.toString() );
        
        try
        {
            response.writeTo( System.out );
            System.out.println();
        }
        catch( Exception ex )
        {
            System.err.println( "error masivo" );
            System.exit( -1 );
        }
    }

    public static void main(String[] args) {
        try
        {
            saludar( "mariano" );
        }
        catch( Exception ex )
        {
            System.err.println( "no se pudo hacer el request saludo, tenes levantado el servidor?" );
        }
        
        try
        {
            sumar( 2,3 );
        }
        catch( Exception ex )
        {
            System.err.println( "no se pudo hacer el request add, tenes levantado el servidor?" );
        }
        
        try
        {
            fac( 92.4 );
        }
        catch( Exception ex )
        {
            System.err.println( "no se pudo hacer el request fahrenheit a celsius, tenes coneccion a internet?" );
        }
    }
}
